package com.runfast.waimai.service.impl;

import com.runfast.common.service.BaseService;
import com.runfast.waimai.dao.mapper.BaseUserMapper;
import com.runfast.waimai.dao.model.BasePurview;
import com.runfast.waimai.dao.model.BaseRole;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.BaseUserExample;
import com.runfast.waimai.service.BaseUserRoleService;
import com.runfast.waimai.service.BaseUserService;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.utils.ComUtil;
import com.runfast.waimai.utils.StringUtil;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class BaseUserServiceImpl extends BaseService<BaseUser, Integer, BaseUserExample> implements BaseUserService {
    @Autowired
    private BaseUserRoleService baseUserRoleService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService runfastAgentbusinessService;

    public List<BaseUser> selectUserByExampleWithPageable(Map map){
        BaseUserMapper baseUserMapper=(BaseUserMapper)this.mapper;
        return baseUserMapper.selectUserByExampleWithPageable(map);
    }
    public Integer countUserByExample(Map map){
        BaseUserMapper baseUserMapper=(BaseUserMapper)this.mapper;
        return baseUserMapper.countUserByExample(map);
    }


    public List<Integer> queryAgentIdsByUser(BaseUser baseUser){
        List<Integer> alist=new ArrayList<Integer>();
        if(!StringUtil.isEmpty(baseUser.getAgentids())){
            String[] strIds = baseUser.getAgentids().split(",");
            for(String strId:strIds){
                if(!StringUtil.isEmpty(strId)){
                    int rid = StringUtil.getInt(strId);
                    if(rid>0){
                        List<Integer> agentIds=runfastAgentbusinessService.findByAgentId(rid);
                        if(agentIds!=null&&agentIds.size()>0){
                            alist.addAll(agentIds);
                        }
                    }
                }
            }
        }
        return alist;
    }

    @Override
    public BaseUser checkByUsername(String username) {
        Validate.notBlank(username);
        BaseUserExample baseUserExample = new BaseUserExample();
        baseUserExample.createCriteria().andUsernameEqualTo(username);
        List<BaseUser> baseUsers = mapper.selectByExample(baseUserExample);
        BaseUser baseUser=new BaseUser();
        if (baseUsers.size()>0){
            baseUser = baseUsers.get(0);
        }else{
            baseUser=null;
        }
        return baseUser;
    }

    public BaseUser checkByUsername1(String username){
        if(!ComUtil.isEmpty(username)){
            BaseUserExample baseUserExample = new BaseUserExample();
            baseUserExample.createCriteria().andUsernameEqualTo(username);
            List<BaseUser> baseUsers = mapper.selectByExample(baseUserExample);
            BaseUser user1=new BaseUser();
            if (baseUsers.size()>0){
                user1 = baseUsers.get(0);
                List<Integer> roleIdList=baseUserRoleService.queryRoleIdList(user1.getId());
                user1.setRoleIdList(roleIdList);
            }else{
                user1=null;
            }
            if(user1!=null){
                initUser(user1);
            }
            return user1;
        }
        return null;
    }

    /**
     * 设置用户
     * @param user1
     */
    private void initUser(BaseUser user1){
        //添加用户所在的组织的下层组织
        /*List<Organization> orgList = null;

        if(user1.getOrgId().equals(appSetting.getRootOrgId())){
            //用户属于最顶层的组织,可以查看所有的组织
            orgList = orgService.findAll();
            user1.setRoot(true);
        }else{
            //否则只能看自己的
            orgList = orgService.findAll(user1.getOrgId());
        }
        //设置自己的组织以及下属组织的ID号
        parseOrgToUser(user1,orgList);
        //设置上级组织ID号
        parseAllOrgToUser(user1,user1.getOrgId());
        //把上级组织ID和下级组织ID合在一起
        user1.getAllOrgIds().addAll(user1.getOrgIds());*/
        //user1.setRoot(true);
        //添加用户自己的权限
        for(BasePurview pur:user1.getPurviews()){
            user1.getPids().add(pur.getPid());
        }
        //把角色的权限也加到权限列表中
        Set<BaseRole> rSet = user1.getRoles();
        user1.getPurviews().size();
        if(rSet.size()>0){
            for(BaseRole role:rSet){
                for(BasePurview pur:role.getPurviews()){
                    user1.getPids().add(pur.getPid());
                }
            }
        }
    }
   public List<BaseUser>  selectUsersByExampleWithPageable(Map<Object,Object> map){
       BaseUserMapper baseUserMapper=(BaseUserMapper)this.mapper;
       return baseUserMapper.selectUsersByExampleWithPageable(map);
    }

    public Integer countUsersByExample(Map map){
        BaseUserMapper baseUserMapper=(BaseUserMapper)this.mapper;
        return baseUserMapper.countUsersByExample(map);
    }

}